<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button icon="el-icon-plus" size="small" type="primary" @click="isshow=true">新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column align="center" type="index" label="序号" width="120" />
              <el-table-column align="center" prop="name" label="名称" width="240" />
              <el-table-column align="center" prop="description" label="描述" />
              <el-table-column align="center" label="操作">
                <template slot-scope="{row}">
                  <el-button size="small" type="success" @click="quanxuan(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                </template>
              </el-table-column>

            </el-table>
            <!-- 放置分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height:60px">
              <el-pagination
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                layout="prev, pager, next"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>
      <!-- 编辑按钮弹出层 -->
      <el-dialog title="编辑弹层" :visible="isshow" @close="btnNo">
        <el-form ref="rulefrom" label-width="120px" :model="rulefrom" :rules="rules">
          <el-form-item prop="name" label="角色名称">
            <el-input v-model="rulefrom.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="rulefrom.description" />
          </el-form-item>
        </el-form>
        <el-row type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnNo">取消</el-button>
            <el-button size="small" type="primary" @click="btnOk">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
        <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
        <!-- id作为唯一标识 -->
        <el-tree
          ref="permTree"
          :data="permData"
          :props="defaultProps"
          :default-expand-all="true"
          show-checkbox
          :check-strictly="true"
          node-key="id"
          :default-checked-keys="selectCheck"
        />
        <!-- 确定 取消 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole, assignPerm } from '@/api/setting'
import { mapGetters } from 'vuex'
import { getPermissionList } from '@/api/permission'
import { transListToTreeData } from '@/utils'
export default {
  data() {
    return {
      list: [],
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 10,
        total: 0 // 记录总数
      },
      formData: {},
      isshow: false,
      rulefrom: {},
      rules: { name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }] },
      permData: [], // 专门用来接收权限数据 树形数据
      defaultProps: {
        label: 'name'
      },
      showPermDialog: false, // 分配权限弹层
      selectCheck: [], // 定义一个数组来接收 已经选中的节点
      roleId: null // 用来记录分配角色的id
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    // 获取角色列表
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.list = rows
      this.page.total = total
    },
    // 底部页面展示
    changePage(newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage // 将当前页码赋值给当前的最新页码
      this.getRoleList() // 重新拉去数据
    },
    // 获取的公司的信息
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
    },
    // 删除角色按钮
    async deleteRole(id) {
      try {
        await this.$confirm('确认删除该角色吗') // 删除角色按钮的提示框确定和取消 ui
        await deleteRole(id)
        this.getRoleList() // 重新调用请求渲染角色列表
        this.$message.success('删除角色成功')
      } catch (error) {
        console.log(error, '删除数据失败')
      }
    },
    // 修改按钮
    async editRole(id) {
      this.rulefrom = await getRoleDetail(id) // 调用接口将id传递到封装方法中
      this.isshow = true // 拿到数据在弹出弹出层页面展示效果会更好流畅
    },
    // 弹出层确定按钮
    async btnOk() {
      try {
        await this.$refs.rulefrom.validate() // 校验整个表单
        // 如果有id为修改角色数据否则为添加角色信息
        if (this.rulefrom.id) {
          await updateRole(this.rulefrom)
        } else {
          // 添加角色
          await addRole(this.rulefrom)
        }
        this.getRoleList() // 重新获取最新的数据,达到页面和数据同步
        this.$message.success('操作成功')
        this.isshow = false // 关闭弹出层
      } catch (error) {
        alert('校验失败')
      }
    },
    // 弹出层取消按钮
    btnNo() {
      this.rulefrom = { // 将表单数据清空
        name: '',
        description: ''
      }
      this.$refs.rulefrom.resetFields() // 清空表单数据
      this.isshow = false // 关闭弹出层
    },
    async quanxuan(id) {
      this.permData = transListToTreeData(await getPermissionList(), '0')
      this.roleId = id
      const { permIds } = await getRoleDetail(id)
      this.selectCheck = permIds
      this.showPermDialog = true
    },
    async btnPermOK() {
      await assignPerm({ permIds: this.$refs.permTree.getCheckedKeys(), id: this.roleId })
      this.$message.success('分配权限成功')
      this.showPermDialog = false
    },
    btnPermCancel() {
      this.selectCheck = []
      this.showPermDialog = false
    }
  }
}
</script>

<style>

</style>
